package com.gxa.meiye.admin.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.meiye.admin.platform.mapper.RoleMapper;
import com.gxa.meiye.admin.platform.pojo.vo.PermVo;
import com.gxa.meiye.admin.platform.pojo.vo.RoleVo;
import com.gxa.meiye.admin.platform.pojo.vo.SetPermVo;
import com.gxa.meiye.admin.platform.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleVo> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<RoleVo> roleList() {
        List<RoleVo> roleVos = roleMapper.selectList(null);
        return roleVos;
    }

    @Override
    public List<RoleVo> selectRoles(String select) {
        LambdaQueryWrapper<RoleVo> roleVoLambdaQueryWrapper = Wrappers.lambdaQuery();
        roleVoLambdaQueryWrapper.like(RoleVo::getName,"%"+select+"%");
        List<RoleVo> roleVos = roleMapper.selectList(roleVoLambdaQueryWrapper);
        return roleVos;
    }

    @Override
    public Integer updateRole(RoleVo roleVo) {
        int i = roleMapper.updateById(roleVo);
        return i;
    }

    @Override
    public Integer delRole(Integer id) {
        int i = roleMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer addRole(RoleVo roleVo) {
        int insert = roleMapper.insert(roleVo);
        return insert;
    }

    @Override
    public Map<String, List<PermVo>> selectPerm(Integer id) {
        List<PermVo> maps = roleMapper.selectPerm();
        for (PermVo permVo :maps) {
            List<Map<String, Object>> maps1 = roleMapper.selectHandle(permVo.getId());
            permVo.setHandle(maps1);
        }
        List<PermVo> permVos = roleMapper.selectPermByUid(id);
        Map<String,List<PermVo>> map = new HashMap<>();
        map.put("所有权限",maps);
        map.put("已拥有权限",permVos);
        return map;
    }

    @Override
    public Integer setPerm(SetPermVo setPermVo) {
        roleMapper.delRoleMenu(setPermVo.getRid());
        for (int i = 0; i < setPermVo.getMIds().size(); i++) {
            roleMapper.addRoleMenu(setPermVo.getRid(),setPermVo.getMIds().get(i));
        }
        return null;
    }
}
